<template>
	<view>
		<button type="default" @click="get">获取数据</button>
		<button type="default" @click="add">添加数据</button>
		<button type="default" @click="update">更新数据</button>
		<view class="list">
			<template v-for="(item,index) in listData">
				<view class="item" :key="index">{{item.name}}--{{item.age}}</view>
			</template>
		</view>
		<view class="paging">
			<text @click="changePage(0)">上一页</text>
			<text>第{{page}}页</text>
			<text @click="changePage(1)">下一页</text>
			<text>每页{{pageSize}}条</text>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				page: 1,
				pageSize: 1,
				listData: [],
				total: 0,
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			async get() {
				const db = uniCloud.database();
				const collection = db.collection('test');
				const testRes = await collection.get();
				const testWhereRes = await collection.where({
					name: '小狗子'
				}).get();
				const dbCmd = db.command; // 取指令
				const testWhereRes2 = await collection.where({
					age: dbCmd.gt(90)
				}).get();
				const testWhereRes3 = await collection.where({
					name: new RegExp('^张')
				}).get();
				const testWhereRes4 = await collection.where({
					name: dbCmd.in(['张三1', '小狗子'])
				}).get();
				const testWhereRes5 = await collection.where({
					name: dbCmd.in(['张三1', '小狗子'])
				}).count();
				const testWhereRes6 = await collection.limit(2).get();
				console.log('获取的数据', testRes.result.data)
				console.log('获取筛选的数据', testWhereRes.result.data)
				console.log('获取筛选的数据2', testWhereRes2.result.data)
				console.log('获取筛选的数据3', testWhereRes3.result.data)
				console.log('获取筛选的数据4', testWhereRes4.result.data)
				console.log('获取数据的数量', testWhereRes5.result.total)
				console.log('限制返回数据的条数', testWhereRes6.result.data)
			},
			async add() {
				const db = uniCloud.database();
				const collection = db.collection('test');
				const testRes = await collection.add([{ name: '张三1', age: 16 }, { name: '张三2', age: 266 }]);
				console.log('获取的数据', testRes)
			},
			async init() {
				uni.showLoading({
					mask: true,
					title:'加载中'
				})
				const db = uniCloud.database();
				const collection = db.collection('test');
				const testRes = await collection.limit(this.pageSize).skip((this.page - 1) * this.pageSize).get();
				const testResCount = await collection.count();
				this.listData = testRes.result.data;
				this.total = testResCount.result.total;
				uni.hideLoading()
			},
			async update(){
				const db = uniCloud.database();
				const collection = db.collection('test');
				let res = await collection.where({name:'小狗子'}).update({
				  name: "新的小狗子",
				  age:999
				});
				console.log(res)
			},
			changePage(val) {
				if (val == 0 && this.page > 1) {
					this.page = this.page - 1;
				} else if (val == 1 && this.page*this.pageSize < this.total) {
					this.page = this.page + 1;
				} else {
					return false;
				}
				this.init();
			}
		}
	}
</script>
<style scoped lang="scss">
	.paging {
		display: flex;
		align-items: center;
		justify-content: center;

		text {
			margin: 0 10rpx;
		}
	}
</style>
